<?php
/**
 * @author: zhouzhongyuan <435690026@qq.com>
 * @copyright 万虎网络
 * @link http://www.wanhunet.com
 */

namespace backend\controllers;

use backend\models\Meetings;
use common\Utils\Info;
use wanhunet;
use yii\data\Pagination;
use Yii;

class MeetingController extends BackendController
{
    /**
     * @return string
     */
    public function actionIndex()
    {
        $data = Meetings::findByUser()->orderBy("id desc")->active();
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => '20']);
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();
        return $this->render('index', [
            "meetings" => $model,
            'pages' => $pages
        ]);
    }

    /**
     * @return string|static
     */
    public function actionAdd()
    {
        $request = wanhunet::$app->request;
        if ($request->isPost) {
            $meeting = new Meetings();
            var_dump($meeting);exit;
            //$meeting->setAttributes($request->post());
            if ($meeting->load($request->post()) && $meeting->save()) {var_dump($request->post());exit;
                return Info::redirectWithInfo([Info::SUCCESS => "添加成功"], ['index']);
            } else {var_dump($request->post());exit;
                return Info::redirectWithInfo($meeting, ['add']);
            }
        }
        return $this->render("add");
    }

    /**
     * @return string
     * @throws \yii\web\NotFoundHttpException
     */
    public function actionView()
    {
        $model = Meetings::findByUserAndId(Yii::$app->request->get('id'));
        return $this->render("view", [
            'meeting' => $model
        ]);
    }

    public function actionDel($id = null)
    {
        $id = intval($id);
        if (($meeting = \src\models\Meetings::findByUserAndId($id)) !== null) {
            /** @var Meetings $meeting */
            $meeting->setStatus(Meetings::STATUS_DELETED);
            if (!$meeting->save()) {
                return json_encode(current($meeting->getFirstErrors()));
            } else {
                return 1;
            }
        } else {
            return 0;
        }
    }

    public function actionRecycle()
    {
        $data = Meetings::findByUser()->orderBy("id desc")->active(false);
        $pages = new Pagination(['totalCount' => $data->count(), 'pageSize' => '20']);
        $model = $data->offset($pages->offset)->limit($pages->limit)->all();

        return $this->render('recycle', [
            "meetings" => $model,
            'pages' => $pages
        ]);
    }

    public function actionRec($id = null)
    {
        $id = intval($id);
        if (($meeting = \src\models\Meetings::findByUserAndId($id)) !== null) {
            /** @var Meetings $meeting */
            $meeting->setStatus(Meetings::STATUS_ACTIVE);
            if (!$meeting->save()) {
                return json_encode(current($meeting->getFirstErrors()));
            } else {
                return 1;
            }
        } else {
            return 0;
        }
    }

    public function actionClean($id = null)
    {
        //&TODO 彻底删除
        $id = intval($id);
        if (($meeting = \src\models\Meetings::findByUserAndId($id)) !== null) {
            /** @var Meetings $meeting */
            if ($meeting->clean()) {
                return 1;
            } else {
                return 0;
            }
        } else {
            return 0;
        }
    }

}